package com.example.blog.controller;

import com.example.blog.model.ArticleInfo;
import com.example.blog.model.UserInfo;
import com.example.blog.service.ArticleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Question ArticleController
 * @Date 2023/1/6 10:14
 * @Solution
 */
@RestController
@RequestMapping("/art")
public class ArticleController {
    int artNum=0;//记录博客数量
    @Autowired
    ArticleService articleService;
    ObjectMapper objectMapper = new ObjectMapper();

    //个人主页获取所有博客数量
    @RequestMapping("/getall")
    public int getAll(HttpServletRequest request, HttpServletResponse response,int state) throws JsonProcessingException {
        //1.获取uid
        HttpSession session = request.getSession();
        int uid = (int)session.getAttribute("user");
        //2.获取到博客List
        List<ArticleInfo> blogList = articleService.getByUid(uid,state);
        return blogList.size();
    }

    //获取所有人的博客数量
    @RequestMapping("/getNum")
    public int getNum(HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
        List<ArticleInfo> blogList = articleService.getAll();
        return blogList.size();
    }

    //获取单篇博客
    @RequestMapping("/getone")
    public ArticleInfo getOne(@RequestParam(value = "blogId",required = false,defaultValue = "0") Integer blogId) throws JsonProcessingException {
        //根据id获取博客
        ArticleInfo articleInfo = articleService.getById(blogId.intValue());
        //增加博客阅读量
        articleService.rcountAdd(blogId);
        //把博客信息返回给前端
        return articleInfo;
    }

    //新增博客
    @RequestMapping("/add")
    public int add(String title,String content,int state,String createtime,String updatetime,HttpServletRequest request) {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = dateFormat.format(date);
        if(createtime==null) {
            createtime=now;
        }
        if(updatetime==null) {
            updatetime=now;
        }
        //获取用户
        HttpSession session = request.getSession();
        int id=(int)session.getAttribute("user");
        return articleService.add(title,content,id,state,createtime,updatetime);
    }

    //删除博客
    @RequestMapping(value = "/del" ,method = RequestMethod.DELETE)
    public int del(Integer blogId) {
        return articleService.del(blogId);
    }

    //草稿箱中发布博客/保存草稿
    @RequestMapping("/revise")
    public void revise(Integer blogId,int state,String title,String content) {
        articleService.updateState(title,content,state,blogId);
    }

    //不分uid直接查询所有博客
    @RequestMapping("/all")
    public List<ArticleInfo> getAllList(HttpServletRequest request, HttpServletResponse response,int state,Integer pindex,Integer psize) {
        if(pindex==null || psize==null) {
            return null;
        }
        //1.获取到博客List
        List<ArticleInfo> blogList = articleService.getAllList(state,pindex,psize);
        for(ArticleInfo articleInfo :blogList) {
            //截取前11个标题
            if(articleInfo.getTitle().length()>11) {
                articleInfo.setTitle(articleInfo.getTitle().substring(0,11)+"...");
            }
            //截取前50个字简介
            if(articleInfo.getContent().length()>90) {
                articleInfo.setContent(articleInfo.getContent().substring(0,90)+"...");
            }
        }
        return blogList;
    }

    //分页查询我的博客
    @RequestMapping("/list")
    public List<ArticleInfo> getList(HttpServletRequest request, HttpServletResponse response,int state,Integer pindex,Integer psize) {
        if(pindex==null || psize==null) {
            return null;
        }
        //1.获取uid
        HttpSession session = request.getSession();
        int uid = (int)session.getAttribute("user");
        //2.获取到博客List
        List<ArticleInfo> blogList = articleService.getList(uid,state,pindex,psize);
        for(ArticleInfo articleInfo :blogList) {
            //截取前11个标题
            if(articleInfo.getTitle().length()>11) {
                articleInfo.setTitle(articleInfo.getTitle().substring(0,11)+"...");
            }
            //截取前50个字
            if(articleInfo.getContent().length()>90) {
                articleInfo.setContent(articleInfo.getContent().substring(0,90)+"...");
            }
        }
        return blogList;
    }
    //获取热门文章
    @RequestMapping("/gethot")
    public ArticleInfo getHot() {
        List<ArticleInfo> list = articleService.getAll();
        //找到阅览量最多的（热门算法：热门值=浏览量+点赞量*2）
        ArticleInfo hot=list.get(0);
        for(ArticleInfo info:list) {
            if(info.getRcount()>hot.getRcount()) {
                hot=info;
            }
        }
        return hot;
    }
}
